﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Lottery.App;
using Lottery.App.BLL;
using System.Threading;
using System.Diagnostics;

namespace UI
{
    public partial class TJ : Form
    {
        private GameMode _gm;
        private LotteryManager _manager = new LotteryManager();
        private string _value;
        private List<string> _values;

        private delegate void HideLoadding();

        public TJ(GameMode gm, string title)
        {
            InitializeComponent();

            this.Text = String.Format("截至{0}{1}", GlobalData.Records[GlobalData.Records.Count - 1].IssueNum, title);
            dgv_list.AutoGenerateColumns = false;
            _gm = gm;

            switch (gm)
            {
                case GameMode.A1:
                case GameMode.A2:
                case GameMode.P2:
                case GameMode.N2:
                case GameMode.D1T2:
                    this.Height = 548; break;
                case GameMode.A3:
                case GameMode.P3:
                case GameMode.N3:
                case GameMode.N4:
                case GameMode.N5:
                case GameMode.B4Z3:
                case GameMode.B5Z4:
                case GameMode.B6Z5:
                default:
                    break;
            }
        }

        private void TJ_Load(object sender, EventArgs e)
        {
            LoadData(OrderBy.ID);
            
        }

        public void LoadData(OrderBy order)
        {
            dgv_list.DataSource = _manager.List(_gm, order);

            dgv_list.Rows[0].Selected = false;
        }

        

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgv_list.SelectedRows.Count > 0)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;

                _value = dgv_list.SelectedRows[0].Cells[0].Value.ToString();

                LoadingManager.Show();

                Thread t = new Thread(ExportMin);
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("请先选中要导出的投注方案", "提示");
            }
        }
        void Do_LoadData()
        {
            string path = new ExportManager().Export(_value, _gm);

            Process.Start(path);

            Invoke(new HideLoadding(HideLoading));
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (dgv_list.SelectedRows.Count > 0)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button5.Enabled = false;

                _value = dgv_list.SelectedRows[0].Cells[0].Value.ToString();
                
                LoadingManager.Show();

                Thread t = new Thread(Do_LoadData);
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("请先选中要导出的投注方案", "提示");
            }
        }
        void ExportMin()
        {
            string path = new ExportManager().ExportMin(_value, _gm);

            Process.Start(path);

            Invoke(new HideLoadding(HideLoading));
        }

        void HideLoading()
        {
            button1.Enabled = true;
            button2.Enabled = true;
            button5.Enabled = true;
            LoadingManager.Hide();
        }

        void ExportLimit()
        {
            string path = new ExportManager().ExportLimit(_values, _gm);

            Process.Start(path);

            Invoke(new HideLoadding(HideLoading));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dgv_list.SelectedRows.Count > 50)
            {
                MessageBox.Show("一次性导出不得大于50条!", "提示");
                return;
            }

            switch (_gm)
            {
                case GameMode.N2:
                case GameMode.N3:
                case GameMode.N4:
                case GameMode.N5:
                case GameMode.D1T2:
                case GameMode.B4Z3:
                case GameMode.B5Z4:
                case GameMode.B6Z5:
                    break;
                default:
                    MessageBox.Show("目前只支持 任二、任三、任四、任五、一胆二拖、买四中三、买五中四、买六中五 的多列导出!", "提示");
                    return;
            }

            if (dgv_list.SelectedRows.Count > 0)
            {
                button2.Enabled = false;
                button5.Enabled = false;
                button1.Enabled = false;

                _values = new List<string>(dgv_list.SelectedRows.Count);
                for (int i = 0; i < dgv_list.SelectedRows.Count; i++)
                {
                    _values.Add(dgv_list.SelectedRows[i].Cells[0].Value.ToString());
                }
                _values.Sort();

                LoadingManager.Show();

                Thread t = new Thread(ExportLimit);
                t.IsBackground = true;
                t.Start();
            }
            else
            {
                MessageBox.Show("请先选中要导出的投注方案", "提示");
            }
        }


        #region 列排序

        private void btnID_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.ID);

            ResetColumnColor(0);
        }

        private void btnOmit_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.CurrentOmit);

            ResetColumnColor(1);
        }

        private void btnMaxOmit_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.MaxOmit);

            ResetColumnColor(2);
        }

        private void btnDate_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.Date);

            ResetColumnColor(3);
        }

        private void btnTotalCount_Click(object sender, EventArgs e)
        {
            LoadData(OrderBy.TotalCount);

            ResetColumnColor(4);
        }


        private void ResetColumnColor(int column)
        {
            dgv_list.Columns[0].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[1].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[2].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[3].DefaultCellStyle.ForeColor = Color.Black;
            dgv_list.Columns[4].DefaultCellStyle.ForeColor = Color.Black;

            dgv_list.Columns[column].DefaultCellStyle.ForeColor = Color.Blue;
        }
        #endregion

        private void dgv_list_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //this method overrides the DataGridView's RowPostPaint event
            //in order to automatically draw numbers on the row header cells
            //and to automatically adjust the width of the column containing
            //the row header cells so that it can accommodate the new row
            //numbers,

            //store a string representation of the row number in 'strRowNumber'
            string strRowNumber = (e.RowIndex + 1).ToString();

            //prepend leading zeros to the string if necessary to improve
            //appearance. For example, if there are ten rows in the grid,
            //row seven will be numbered as "07" instead of "7". Similarly, if
            //there are 100 rows in the grid, row seven will be numbered as "007".
            while (strRowNumber.Length < dgv_list.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;

            //determine the display size of the row number string using
            //the DataGridView's current font.
            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);

            //adjust the width of the column that contains the row header cells
            //if necessary
            if (dgv_list.RowHeadersWidth < (int)(size.Width + 20)) dgv_list.RowHeadersWidth = (int)(size.Width + 20);

            //this brush will be used to draw the row number string on the
            //row header cell using the system's current ControlText color
            Brush b = SystemBrushes.ControlText;

            //draw the row number string on the current row header cell using
            //the brush defined above and the DataGridView's default font
            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string value = String.Empty;

            if (checkBox1.Checked) value += "01 ";
            if (checkBox2.Checked) value += "02 ";
            if (checkBox3.Checked) value += "03 ";
            if (checkBox4.Checked) value += "04 ";
            if (checkBox5.Checked) value += "05 ";
            if (checkBox6.Checked) value += "06 ";
            if (checkBox7.Checked) value += "07 ";
            if (checkBox8.Checked) value += "08 ";
            if (checkBox9.Checked) value += "09 ";
            if (checkBox10.Checked) value += "10 ";
            if (checkBox11.Checked) value += "11 ";
            if (checkBox12.Checked) value += "12 ";
            if (checkBox13.Checked) value += "13 ";
            if (checkBox14.Checked) value += "14 ";
            if (checkBox15.Checked) value += "15 ";
            if (checkBox16.Checked) value += "16 ";
            if (checkBox17.Checked) value += "17 ";
            if (checkBox18.Checked) value += "18 ";
            if (checkBox19.Checked) value += "19 ";
            if (checkBox20.Checked) value += "20 ";

            value = value.Trim();

            if (String.IsNullOrEmpty(value))
            {
                MessageBox.Show("请先选择胆码!", "提示");
                return;
            }

            if (value.Length > 17)
            {
                MessageBox.Show("胆码不能大于六个!", "提示");
                return;
            }

            dgv_list.DataSource = _manager.Search(_gm, value);

            ResetColumnColor(1);
        }










    }
}
